package com.ruoyi.driver.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowflakeIdUtils;
import com.ruoyi.driver.domain.TDriverBindVehicleRecords;
import com.ruoyi.driver.mapper.TDriverBindVehicleRecordsMapper;
import com.ruoyi.modules.domain.VehicleInfo;
import com.ruoyi.modules.mapper.VehicleInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.driver.mapper.TDriverBindVehicleMapper;
import com.ruoyi.driver.domain.TDriverBindVehicle;
import com.ruoyi.driver.service.ITDriverBindVehicleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 司机绑定车辆Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-26
 */
@Service
public class TDriverBindVehicleServiceImpl implements ITDriverBindVehicleService 
{
    @Autowired
    private TDriverBindVehicleMapper tDriverBindVehicleMapper;


    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;


    @Autowired
    private TDriverBindVehicleRecordsMapper tDriverBindVehicleRecordsMapper;

    /**
     * 查询司机绑定车辆
     * 
     * @param id 司机绑定车辆主键
     * @return 司机绑定车辆
     */
    @Override
    public TDriverBindVehicle selectTDriverBindVehicleById(String id)
    {
        return tDriverBindVehicleMapper.selectTDriverBindVehicleById(id);
    }

    /**
     * 查询司机绑定车辆列表
     * 
     * @param tDriverBindVehicle 司机绑定车辆
     * @return 司机绑定车辆
     */
    @Override
    public List<TDriverBindVehicle> selectTDriverBindVehicleList(TDriverBindVehicle tDriverBindVehicle)
    {
        return tDriverBindVehicleMapper.selectTDriverBindVehicleList(tDriverBindVehicle);
    }

    /**
     * 新增司机绑定车辆
     * 
     * @param tDriverBindVehicle 司机绑定车辆
     * @return 结果
     */
    @Override
    public int insertTDriverBindVehicle(TDriverBindVehicle tDriverBindVehicle)
    {
        tDriverBindVehicle.setCreateTime(DateUtils.getNowDate());
        return tDriverBindVehicleMapper.insertTDriverBindVehicle(tDriverBindVehicle);
    }

    /**
     * 修改司机绑定车辆
     * 
     * @param tDriverBindVehicle 司机绑定车辆
     * @return 结果
     */
    @Override
    public int updateTDriverBindVehicle(TDriverBindVehicle tDriverBindVehicle)
    {
        tDriverBindVehicle.setUpdateTime(DateUtils.getNowDate());
        return tDriverBindVehicleMapper.updateTDriverBindVehicle(tDriverBindVehicle);
    }

    /**
     * 批量删除司机绑定车辆
     * 
     * @param ids 需要删除的司机绑定车辆主键
     * @return 结果
     */
    @Override
    public int deleteTDriverBindVehicleByIds(String[] ids)
    {
        return tDriverBindVehicleMapper.deleteTDriverBindVehicleByIds(ids);
    }

    /**
     * 删除司机绑定车辆信息
     * 
     * @param id 司机绑定车辆主键
     * @return 结果
     */
    @Override
    public int deleteTDriverBindVehicleById(String id)
    {
        return tDriverBindVehicleMapper.deleteTDriverBindVehicleById(id);
    }

    /**
     * 上车
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upperCar(TDriverBindVehicle driverBindVehicle) {
        int row = 0;
        // 司机绑定车辆表
        SnowflakeIdUtils idWorker = new SnowflakeIdUtils(1, 3);
        driverBindVehicle.setId(idWorker.nextId()+"");
        driverBindVehicle.setCreateTime(DateUtils.getNowDate());
        driverBindVehicle.setOnCarTime(DateUtils.getNowDate());
        //绑定状态:0.上车（绑定） 1.下车（解绑）
        driverBindVehicle.setOnOffStatus("0");
        row = tDriverBindVehicleMapper.insertTDriverBindVehicle(driverBindVehicle);

        // 司机绑定车辆记录表
        TDriverBindVehicleRecords records = new TDriverBindVehicleRecords();
        SnowflakeIdUtils idWorker1 = new SnowflakeIdUtils(1, 3);
        records.setId(idWorker1.nextId()+"");
        records.setCreateTime(DateUtils.getNowDate());
        records.setOnOffCarTime(DateUtils.getNowDate());
        records.setDriverBindVehicleId(driverBindVehicle.getId());

        records.setVehicleInfoId(driverBindVehicle.getVehicleInfoId());
        records.setDriverInfoId(driverBindVehicle.getDriverInfoId());
        records.setCarCode(driverBindVehicle.getCarCode());
        records.setTransportationCompanyId(driverBindVehicle.getTransportationCompanyId());
        records.setTeamId(driverBindVehicle.getTeamId());
        records.setTransportationCompanyName(driverBindVehicle.getTransportationCompanyName());
        //上下车状态:0.上车 1.下车
        records.setOnOffStatus("0");
        records.setDriverUserId(driverBindVehicle.getDriverUserId());
        row = tDriverBindVehicleRecordsMapper.insertTDriverBindVehicleRecords(records);


        // 车辆信息id
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setId(driverBindVehicle.getVehicleInfoId());
        vehicleInfo.setCarUsageStatus(driverBindVehicle.getCarUsageStatus());
        vehicleInfo.setCreateTime(DateUtils.getNowDate());

        row = vehicleInfoMapper.updateVehicleInfo(vehicleInfo);

        return row;
    }

    /**
     * 下车
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int lowerCar(TDriverBindVehicle driverBindVehicle) {
        int row = 0;
        // 司机绑定车辆
        driverBindVehicle.setUpdateTime(DateUtils.getNowDate());
        driverBindVehicle.setOnCarTime(DateUtils.getNowDate());
        driverBindVehicle.setOffCarTime(DateUtils.getNowDate());
        //绑定状态:0.上车（绑定） 1.下车（解绑）
        driverBindVehicle.setOnOffStatus("1");
        row = tDriverBindVehicleMapper.updateTDriverBindVehicle(driverBindVehicle);


        // 司机绑定车辆记录表
        TDriverBindVehicleRecords records = new TDriverBindVehicleRecords();
        SnowflakeIdUtils idWorker1 = new SnowflakeIdUtils(1, 3);
        records.setId(idWorker1.nextId()+"");
        records.setCreateTime(DateUtils.getNowDate());
        records.setOnOffCarTime(DateUtils.getNowDate());
        records.setDriverBindVehicleId(driverBindVehicle.getId());

        records.setVehicleInfoId(driverBindVehicle.getVehicleInfoId());
        records.setDriverInfoId(driverBindVehicle.getDriverInfoId());
        records.setCarCode(driverBindVehicle.getCarCode());
        records.setTransportationCompanyId(driverBindVehicle.getTransportationCompanyId());
        records.setTeamId(driverBindVehicle.getTeamId());
        records.setTransportationCompanyName(driverBindVehicle.getTransportationCompanyName());
        //上下车状态:0.上车 1.下车
        records.setOnOffStatus("1");
        records.setDriverUserId(driverBindVehicle.getDriverUserId());
        row = tDriverBindVehicleRecordsMapper.insertTDriverBindVehicleRecords(records);


        // 车辆信息id
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setId(driverBindVehicle.getVehicleInfoId());
        vehicleInfo.setCarUsageStatus(driverBindVehicle.getCarUsageStatus());
        vehicleInfo.setUpdateTime(DateUtils.getNowDate());
        row = vehicleInfoMapper.updateVehicleInfo(vehicleInfo);
        return row;
    }
}
